/*
 * @Author: zhang peng
 * @Date: 2021-08-03 10:57:51
 * @LastEditTime: 2023-04-12 17:14:19
 * @LastEditors: rongkangwen
 * @Description:
 * @FilePath: /uniapp-h5/polyfill/mixins.js
 *
 * 如果你想删除本文件，请先确认它使用的范围，感谢合作~
 * 如有疑问，请直接联系: 375890534@qq.com
 */
const https_1 = require('../api/https.js');
var globalData_1 = require('../globalData');

console.log(globalData_1,'这是什么呢啦啦啦')
import { isSellerXc,isZshAppInH5,isZsjMiniInH5,isZsjInH5,isxmgjMix,iscy,isJw,isnylH5,isChangzhi,isCsht,isLgxny,isCdgj,isCqym,isTjhz,isChangtong,isCYCAPPInH5,isAIKC } from '../utils/util'
export default {
	data() {
		return {
			marketingSwitch: !!globalData_1.default.marketingSwitch,
			sellerNo: globalData_1.default.sellerNo,
			sellerId: globalData_1.default.sellerId,
			pageApiObj: false,
			objColor: '',
			objColor2: {
				textColor: '#ff0000'
			},
			mixIconList: [],
			mixDiscountIconList: [],
			mixRechargeIconList: [],
			mixPowerIconList: [],
			mixOtherIconList: [],
			baseConfig: {},
			minxins_csSwitch:false,
			isOpenDev: globalData_1.default.isOpenDev,
			isV2GOption: globalData_1.default.isV2GOption, // #V2G功能
			batteryCheckSwitch: globalData_1.default.batteryCheckSwitch, // 电池检测功能
			prepaidCardSwitch: globalData_1.default.prepaidCardSwitch, // 储值卡功能开关
			advertisingSwitch: globalData_1.default.advertisingSwitch, // 广告功能开关
			doubleGunSwitch: globalData_1.default.doubleGunSwitch // 双枪功能开关
		}
	},
	// 定义全局分享
	// 1.发送给朋友
	onShareAppMessage(res) {
		return {
			path: '/pages/index/index',
			title: globalData_1.default.name,
		}
	},
	//2.分享到朋友圈
	onShareTimeline(res) {
		return {
			path: '/pages/index/index',
			title: globalData_1.default.name,
		}
	},
	onLoad() {

	},
	onShow() {

		setTimeout(() => {

			// 开发工具下不重复请求默认配置
			// if(getApp().getEnvVersion() == 'develop'){
			// 	if(getApp().defaultConfig){
			// 		console.log('再次走到这里了，不请求配置')
			// 		return
			// 	}else{
			// 		console.log('走到这里了，下次不请求配置')
			// 		getApp().defaultConfig = true
			// 	}
			// }

			this.marketingSwitch = !!globalData_1.default.marketingSwitch
			https_1
				.getIconTwo({
					twoEnum: 'HUE',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						this.objColor = `--textColor:${res.data[0].icon};--btnColor:${res.data[1].icon};`
						this.objColor2 = {
							textColor: res.data[0].icon,
							btnColor: res.data[1].icon,
						}
						uni.setStorageSync('textColor', this.objColor2.textColor)
					}
				});
			this.getConfig();

			this.getIcon();


			uni.getNetworkType({
				success: res => {
					// console.log('网络状态', res.networkType); //网络类型 wifi、2g、3g、4g、ethernet、unknown、none
					if (res.networkType === 'none') {
						if (getCurrentPages()[getCurrentPages().length - 1].route !==
							'pages/errorPage/index') {
							uni.showModal({
								title: '系统提示',
								content: '你的网络好像不给力，请检查网络状况，稍后再试',
								showCancel: false,
								success: res => {
									if (res.confirm) {}
								}
							});
							uni.navigateTo({
								url: '/pages/errorPage/index?code=-1'
							});
						}
						console.log('当前无网络');
					} else {
						console.log('有网络');
					}
				}
			});
		}, 300)
	},
	methods: {
		isSellerXc,
		isZsjInH5,
		isZsjMiniInH5,
		isZshAppInH5,
		isxmgjMix,
		iscy,
		isJw,
		isnylH5,
		isChangzhi,
		isCsht,
		isLgxny,
		isCdgj,
		isCqym,
		isTjhz,
		isCYCAPPInH5,
		isChangtong,
		isAIKC,
		toJSON(){
			return this
		},
		// 获取平台配置
		getConfig() {
			const that = this
			https_1
				.getSellerBasicsConfig({
					sellerId: getApp().globalData.sellerId
				})
				.then(res => {
					console.log('获取平台设置的商家信息配置...', res.data)
					that.baseConfig = res.data
					// if(res.code == 0){
					getApp().globalData.baseConfig = res.data
					uni.setStorageSync('mobilePlatformName', res.data.mobilePlatformName)
					uni.setStorageSync('mobilePlatformLogo', res.data.mobilePlatformLogo)
					// uni.setStorageSync('platformLogo', res.data.platformLogo)
					uni.setStorageSync('mobilePlatformFilingDesc', res.data.mobilePlatformFilingDesc)
					uni.setStorageSync('mobilePlatformFilingLink', res.data.mobilePlatformFilingLink)
					// }
				})
		},

		//获取公共icon
		getIcon() {
			// if (!getApp().globalData.iconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'PERSONAL_CENTER',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						this.mixIconList = res.data
						getApp().globalData.iconList = res.data
					}
				});
			// } else {
			// 	this.mixIconList = getApp().globalData.iconList
			// }

			// if (!getApp().globalData.tabbarIconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'BASE_NAVIGATION_MODULE',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						getApp().globalData.tabbarIconList = res.data
						uni.setStorageSync('tabbarIconList', res.data)
					}
				});
			// }
			// if (!getApp().globalData.discountIconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'DISCOUNT_COUPON_MODULE',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						this.mixDiscountIconList = res.data
						getApp().globalData.discountIconList = res.data
					}
				});
			// } else {
			// 	this.mixDiscountIconList = getApp().globalData.discountIconList
			// }
			// if (!getApp().globalData.rechargeIconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'BALANCE_RECHARGE',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						this.mixRechargeIconList = res.data
						getApp().globalData.rechargeIconList = res.data
					}
				});
			// } else {
			// 	this.mixRechargeIconList = getApp().globalData.rechargeIconList
			// }
			// if (!getApp().globalData.powerIconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'MY_BILL',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						this.mixPowerIconList = res.data
						getApp().globalData.powerIconList = res.data
					}
				});
			// } else {
			// 	this.mixPowerIconList = getApp().globalData.powerIconList
			// }
			// if (!getApp().globalData.otherIconList.length) {
			https_1
				.getIconTwo({
					twoEnum: 'OTHER',
					sellerId: getApp().globalData.sellerId
				})
				.then((res) => {
					if (res.code === 0) {
						console.log(res, '默认配置')
						this.mixOtherIconList = res.data
						getApp().globalData.otherIconList = res.data
						uni.setStorageSync('otherIconList', res.data)

					}
				});
			// } else {
			// 	this.mixOtherIconList = getApp().globalData.otherIconList
			// }
		},
		/**
		 * 转义符换成普通字符
		 * @param {*} str
		 * @returns
		 */
		escape2Html(str) {
			if (!str) return str
			var arrEntities = {
				'lt': '<',
				'gt': '>',
				'nbsp': ' ',
				'amp': '&',
				'quot': '"'
			}
			return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
				return arrEntities[t]
			})
		},
		/**
		 * 普通字符转换成转义符
		 * @param {*} sHtml
		 * @returns
		 */
		html2Escape(sHtml) {
			if (!sHtml) return sHtml
			return sHtml.replace(/[<>&"]/g, function(c) {
				return {
					'<': '&lt;',
					'>': '&gt;',
					'&': '&amp;',
					'"': '&quot;'
				} [c]
			})
		},
		/**
		 * setData polyfill 勿删!!!
		 * 用于转换后的uniapp的项目能直接使用this.setData()函数
		 * @param {*} obj
		 * @param {*} callback
		 */
		setData: function(obj, callback) {
			let that = this
			const handleData = (tepData, tepKey, afterKey) => {
				var tepData2 = tepData
				tepKey = tepKey.split('.')
				tepKey.forEach(item => {
					if (tepData[item] === null || tepData[item] === undefined) {
						let reg = /^[0-9]+$/
						tepData[item] = reg.test(afterKey) ? [] : {}
						tepData2 = tepData[item]
					} else {
						tepData2 = tepData[item]
					}
				})
				return tepData2
			}
			const isFn = function(value) {
				return typeof value == 'function' || false
			}
			Object.keys(obj).forEach(function(key) {
				let val = obj[key]
				key = key.replace(/\]/g, '').replace(/\[/g, '.')
				let front, after
				let index_after = key.lastIndexOf('.')
				if (index_after != -1) {
					after = key.slice(index_after + 1)
					front = handleData(that, key.slice(0, index_after), after)
				} else {
					after = key
					front = that
				}
				if (front.$data && front.$data[after] === undefined) {
					Object.defineProperty(front, after, {
						get() {
							return front.$data[after]
						},
						set(newValue) {
							front.$data[after] = newValue
							that.hasOwnProperty("$forceUpdate") && that.$forceUpdate()
						},
						enumerable: true,
						configurable: true
					})
					front[after] = val
				} else {
					that.$set(front, after, val)
				}
			})
			// this.$forceUpdate();
			isFn(callback) && this.$nextTick(callback)
		},
		/**
		 * 解析事件里的动态函数名，这种没有()的函数名，在uniapp不被执行
		 * 比如：<view bindtap="{{openId==undefined?'denglu':'hy_to'}}">立即</view>
		 * @param {*} exp
		 */
		parseEventDynamicCode(e, exp) {
			if (typeof(this[exp]) === 'function') {
				this[exp](e)
			}
		},
		/**
		 * 用于处理对props进行赋值的情况
		 * //简单处理一下就行了
		 *
		 * @param {*} target
		 * @returns
		 */
		deepClone(target) {
			return JSON.parse(JSON.stringify(target))
		},
		/**
		 * 用于处理dataset
		 * 自定义组件的事件里，是获取不到e.currentTarget.dataset的
		 * 因此收集data-参数，手动传进去
		 *
		 * @param {*} event
		 * @param {*} dataSet
		 * @returns
		 */
		datasetHandle(event, dataSet = {}) {
			if (event && !event.currentTarget) {
				if (dataSet.tagId) {
					event.currentTarget = {
						id: dataSet.tagId
					}
				} else {
					event.currentTarget = {
						dataset: dataSet
					}
				}
			}
		}
	}
}